from PyQt5.QtWidgets import QDialog, QLineEdit, QLabel, QApplication, QPushButton, QCheckBox
from PyQt5.QtCore import Qt, QSize
import sys
from PyQt5.QtGui import QFontDatabase, QPixmap
from Dialog import WarningMessageBox

styleSheet = """
.lginLineEdit::hover{
color:green;border:1px solid;
 border-color:#6495ed;
 }
    
.lginLineEdit{
border: 1px solid #ffe4e1;
 font: normal 13px 'FontAwesome'
 }
 
.lginWidgetBtn{
    border: 1px solid #E9967A;
    border-radius: 15px;
    background-color: 	#8FBC8F;
    color: white;
    font-family: "Microsoft YaHei"

}
.lginWidgetBtn::hover{
    background-color: #E9967A
}

.lginWidgetBtn::pressed{
    background-color: #8FBC8F
}

LoginDialog{
    background-color: white;
}

 """


class LoginDialog(QDialog):
    def __init__(self, parent=None):
        super(LoginDialog, self).__init__(parent=parent)
        self._passward = ""
        self._ID = ""
        self._IDLE = QLineEdit(self)

        self._IDLE.setPlaceholderText("\uf007  请输入用户名")
        self._IDLE.setProperty("class", "lginLineEdit")
        self._passwardLE = QLineEdit(self)
        self._passwardLE.setProperty("class", "lginLineEdit")
        self._IDLE.setFixedSize(333, 40)
        self._passwardLE.setFixedSize(333, 40)
        self._passwardLE.setPlaceholderText("\uf13e  请输入密码")
        self._IDLE.move(66, 147)
        self._passwardLE.move(66, 200)
        self._passwardLE.setEchoMode(QLineEdit.Password)

        self._lginBtn = QPushButton(self)
        self._cancelBtn = QPushButton(self)
        self._lginBtn.setText("登录")
        self._cancelBtn.setText("取消")
        self._lginBtn.setProperty("class", "lginWidgetBtn")
        self._cancelBtn.setProperty("class", "lginWidgetBtn")
        self._lginBtn.move(66, 280)
        self._cancelBtn.move(253, 280)
        self._lginBtn.setMinimumSize(147, 50)
        self._cancelBtn.setMinimumSize(147, 50)
        self._lginBtn.clicked.connect(self._onLgin)
        self._cancelBtn.clicked.connect(self._onCancel)

        self._imgLabel = QLabel(self)
        self._imgLabel.setFixedSize(120, 120)
        self._imgLabel.move(173, 20)
        pix = QPixmap("../Data/logo.jpg")
        pix = pix.scaled(QSize(self._imgLabel.width(), self._imgLabel.height()))
        self._imgLabel.setPixmap(pix)

        self._checkbox = QCheckBox(self)
        self._checkbox.setText("记住密码")
        self._checkbox.move(306, 253)

        self.setFixedSize(466, 386)
        self.setAttribute(Qt.WA_StyledBackground, True)
        self.setWindowFlags(self.windowFlags() | Qt.FramelessWindowHint)

    def _onLgin(self):
        self._ID = self._IDLE.text()
        self._passward = self._passwardLE.text()
        self.done(QDialog.Accepted)

    def _onCancel(self):
        self.done(QDialog.Rejected)

    def getPassward(self):
        return self._passward

    def getID(self):
        return self._ID

    def isRemember(self):
        return self._checkbox.isChecked()

    def setRemember(self, status):
        self._checkbox.setChecked(status)

    def setID(self, ID):
        self._IDLE.setText(ID)

    def setPassword(self, password):
        self._passwardLE.setText(password)

def lgin():
    da = LoginDialog()
    if da.exec():
        if da.getID() == "" or da.getPassward() == "":
            WarningMessageBox("用户名、密码不能为空").exec()
            lgin()
        else:
            if da.getID() == "一线生机" and da.getPassward() == "123456":
                return 1
            else:
                WarningMessageBox("用户名、密码不正确").exec()
                lgin()
    else:
        sys.exit(0)

def main():
    app = QApplication(sys.argv)
    fontId = QFontDatabase.addApplicationFont(
        "../Data/fontawesome-webfont.ttf")
    app.setStyleSheet(styleSheet)
    lgin()

    sys.exit(app.exec())

if __name__ == "__main__":
    main()
